package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserSkinVolumeRewardData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/4/29 15:54
 */
public class UserSkinVolumeRewardDataDaoImpl extends TempDataObjectDao<UserSkinVolumeRewardData> {
    public static String insertSql = "insert into t_u_skin_volume_reward(userId,volumeId,getRewardIndexList,getRewardTime) values (?,?,?,?)";
    @Override
    protected boolean addData(UserSkinVolumeRewardData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getVolumeId()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getGetRewardIndexList(), ",")));
        para.put(i++, new DbParameter(Types.DATE, data.getGetRewardTime()));
        return getDb().executeNoneQuery(insertSql, para) > 0;
    }

    public static String updateSql = "update t_u_skin_volume_reward set getRewardIndexList=?, getRewardTime=? where userId=? and volumeId=?";
    @Override
    protected boolean updateData(UserSkinVolumeRewardData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getGetRewardIndexList(), ",")));
        para.put(i++, new DbParameter(Types.DATE, data.getGetRewardTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getVolumeId()));
        return getDb().executeNoneQuery(updateSql, para) > 0;
    }

    @Override
    protected UserSkinVolumeRewardData parseData(ResultSet rs) throws SQLException {
        UserSkinVolumeRewardData data = new UserSkinVolumeRewardData();
        data.setUserId(rs.getLong("userId"));
        data.setVolumeId(rs.getInt("volumeId"));
        data.setGetRewardIndexList(StringUtils.stringToIntegerList(rs.getString("getRewardIndexList"), ","));
        data.setGetRewardTime(rs.getTimestamp("getRewardTime"));
        return data;
    }
    public static String getSql = "select * from t_u_skin_volume_reward where userId=?";
    public Map<Integer, UserSkinVolumeRewardData> getSkinVolumeRewardData(long userId) {

        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserSkinVolumeRewardData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserSkinVolumeRewardData rewardData = parse(rs);
                map.put(rewardData.getVolumeId(), rewardData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }
}
